<template>
  <div id="detail">
    <!-- 抽屉   -->
    <el-drawer
        title="类型细节展示"
        :visible.sync="drawerVisible"
        direction="rtl"
        :before-close="handleDrawerClose"
        size="40%">

      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="detail.filters">
          <el-form-item>
            <el-input v-model="detail.filters.keywords" placeholder="细节名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="keywordsQuery2">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd2">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表-->
      <el-table :data="detail.dirDetail" highlight-current-row v-loading="detail.listLoading" @selection-change="selsChange2" style="width: 100%;">
        <el-table-column type="selection" width="80">
        </el-table-column>
        <el-table-column prop="name" label="细节名称" width="300px">
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" @click="handleEdit2(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel2(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove2" :disabled="this.detail.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange2" :current-page="detail.currentPage" :page-size="detail.pageSize" :total="detail.totals" style="float:right;">
        </el-pagination>
      </el-col>

    </el-drawer>

    <!-- 抽屉的添加或修改的对话框 -->
    <el-dialog :title="title" :visible.sync="detail.saveFormVisible" :close-on-click-modal="false">
      <el-form :model="detail.saveForm" label-width="80px" ref="saveForm2">
        <el-form-item label="细节名称">
          <el-input v-model="detail.saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="detail.saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit2">提交			</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      // 抽屉类型
      title: '',
      currentTypeId: null,
      detail: {
        filters: {
          keywords: ''
        },
        dirDetail: [],
        totals: 0,
        currentPage: 1,
        pageSize: 5,
        listLoading: false,
        sels: [],//列表选中列
        saveFormVisible: false,//编辑界面是否显示
        // 添加或界面数据
        saveForm: {
          id: null,
          name: '',
          types_id: null
        }
      },
      drawerVisible: false
    }
  },
  methods: {
    // 抽屉方法
    keywordsQuery2(){
      this.detail.currentPage = 1;
      this.getDirDetail();
    },
    handleCurrentChange2(val) {
      this.detail.currentPage = val;
      this.getDirDetail();
    },
    //获取用户列表
    getDirDetail() {
      let para = {
        typeId: this.currentTypeId,
        currentPage: this.detail.currentPage,
        pageSize: this.detail.pageSize,
        keywords: this.detail.filters.keywords
      };
      this.detail.listLoading = true;
      this.$http.post("/dictionaryDetail",para).then(result=>{
        this.detail.totals = result.data.totals;
        this.detail.dirDetail = result.data.data;
        this.detail.listLoading = false;
      }).catch(result=>{
        this.$message.error('请求错误');
      })
    },
    //删除
    handleDel2: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.detail.listLoading = true;
        this.$http.delete("/dictionaryDetail/"+row.id).then(res =>{
          this.$message(res.data.msg);
          this.detail.currentPage = 1;
          this.getDirDetail();
          this.detail.listLoading = false;
        }).catch(res =>{
          this.$message({
            message: '系统错误，稍后尝试',
            type: 'error'
          });
          this.detail.currentPage = 1;
          this.getDirDetail();
          this.detail.listLoading = false;
        })
      }).catch(() => {
      });
    },
    //显示编辑界面
    handleEdit2: function (index, row) {
      this.title='编辑';
      this.detail.saveForm = Object.assign({}, row);
      this.detail.saveFormVisible = true;
    },
    //显示新增界面
    handleAdd2: function () {
      this.title='新增';
      this.detail.saveForm = {
        id: null,
        name: '',
        types_id: this.currentTypeId
      };
      this.detail.saveFormVisible = true;
    },
    // 提叫添加或修改
    saveSubmit2: function () {
      this.$refs.saveForm2.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            let para = Object.assign({}, this.detail.saveForm);
            this.$http.put("/dictionaryDetail/", para).then(res => {
              this.$message(res.data.msg);
              this.detail.saveFormVisible = false;
              this.getDirDetail();
            }).catch(res => {
              this.$message({
                message: '系统错误，稍后尝试',
                type: 'error'
              });
              this.detail.saveFormVisible = false;
              this.getDirDetail();
            });
          });
        }
      });
    },
    // 批量选择时参数
    selsChange2: function (sels) {
      this.detail.sels = sels;
    },
    //批量删除
    batchRemove2: function () {
      var ids = this.detail.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.detail.listLoading = true;
        this.$http.patch("/dictionaryDetail/", ids).then(res =>{
          this.$message(res.data.msg);
          this.detail.currentPage = 1;
          this.detail.listLoading = false;
          this.getDirDetail();
        }).catch(res =>{
          this.$message({
            message: '系统错误，稍后尝试',
            type: 'error'
          });
          this.detail.currentPage = 1;
          this.detail.listLoading = false;
          this.getDirDetail();
        })
      }).catch(() => {
      });
    },

    drawerOpen(id){
      this.currentTypeId = id;
      this.getDirDetail();
      this.drawerVisible = true;
    },
    // 关闭前操作
    handleDrawerClose(done){
      this.$confirm('确认关闭？')
          .then(_ => {
            this.currentTypeId = null;
            done();
          }).catch(_ => {});
    }
  }
}
</script>

<style scoped>

</style>